package com.hxz.djbxkj.HxzExecutor.ExecutorCode.Run;

import com.hxz.djbxkj.HxzExecutor.ExecutorCode.JobJk;
import com.hxz.djbxkj.HxzExecutor.ExecutorCode.Run.Abstract.AopJk;
import com.hxz.djbxkj.HxzExecutor.ExecutorCode.Run.Abstract.DoPlayMethodByHxzMethod;
import com.hxz.djbxkj.HxzExecutor.Model.JobMode;
import com.hxz.djbxkj.HxzExecutor.Model.ParameterMode;
import com.hxz.djbxkj.HxzExecutor.zy.RunType;

public abstract class RunByHxzMethod extends DoPlayMethodByHxzMethod implements ParameterJk,Runnable {
    protected String id;
    protected ParameterMode parameter;
    protected JobJk job;
    protected AopJk aop;
    protected JobMode jobMode;

    public RunByHxzMethod(JobJk jk, ParameterMode pm, String id,JobMode jm,AopJk aop) throws Exception{
        this.id=id;
        this.job=jk;
        this.parameter=pm;
        this.jobMode=jm;
        this.aop=aop;
        if(!super.CkJobJk(this.job,this.id)) throw new Exception("必须使用@HxzMethod注解修饰函数,且id:["+this.id+"]必须唯一");
    }

    public String getId() {
        return id;
    }

    public ParameterMode getParameter() {
        return parameter;
    }

    public JobJk getJob() {
        return job;
    }

    public JobMode getJobMode() {
        return jobMode;
    }


    public final void befor() {
        this.getJobMode().setBegin().setState(RunType.ZXZ);
        if(null!=aop){
            try{
                aop.befor(jobMode);
            }catch (Exception e){
                errer(e);
            }
        }
    }

    public final void after(Object o) {
        this.getJobMode().put("data",o);
        if(null!=aop){
            try{
                aop.after(o,jobMode);
            }catch (Exception e){
                errer(e);
            }
        }
        this.getJobMode().setState(RunType.ZXW).setEnd();
    }

    public final void errer(Exception e) {
        this.getJobMode().appendErrerMsg(e.getMessage()).appendErrerMsg(e.getLocalizedMessage());
        e.printStackTrace();
        try{
            aop.errer(e,jobMode);
        }catch (Exception ex){
            this.getJobMode().appendErrerMsg(ex.getMessage()).appendErrerMsg(ex.getLocalizedMessage());
            ex.printStackTrace();
        }
        this.getJobMode().setState(RunType.ZXSB).setEnd();

    }

}
